package com.pasabahceambalaj.session;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.framework.EntityHome;

import com.pasabahceambalaj.entity.Firma;
import com.pasabahceambalaj.entity.Kalip;
import com.pasabahceambalaj.entity.Kullanici;
import com.pasabahceambalaj.entity.Urun;

@Name("urunHome")
public class UrunHome extends EntityHome<Urun> {

	@In(create = true)
	FirmaHome firmaHome;
	@In(create = true)
	KalipHome kalipHome;

	public void setUrunId(Long id) {
		setId(id);
	}

	public Long getUrunId() {
		return (Long) getId();
	}

	@Override
	protected Urun createInstance() {
		Urun urun = new Urun();
		return urun;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		
		Firma firma = null;
		if (Authenticator.isAdmin()) {
			firma = firmaHome.getDefinedInstance();
		} else {
			firma = Authenticator.getFirma();
		}
		if (firma != null) {
			getInstance().setFirma(firma);
		}

		Kalip kalip = kalipHome.getDefinedInstance();
		if (kalip != null) {
			getInstance().setKalip(kalip);
		}
	}

	public boolean isWired() {
		if (getInstance().getFirma() == null)
			return false;
//		if (getInstance().getKalip() == null)
//			return false;
		return true;
	}

	public Urun getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

}
